package com.choichumgming.rentcat.service.Impl;


import com.choichumgming.rentcat.mapper.UmsMemberReceiveAddressMapper;
import com.choichumgming.rentcat.model.UmsMemberReceiveAddress;
import com.choichumgming.rentcat.model.UmsMemberReceiveAddressExample;
import com.choichumgming.rentcat.service.UmsMemberReceiveAddressService;
import com.choichumgming.rentcat.service.UmsMemberService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 用户地址管理Service实现类
 */
@Slf4j
@Service
public class UmsMemberReceiveAddressServiceImpl implements UmsMemberReceiveAddressService {
    @Autowired
    private UmsMemberService memberService;
    @Autowired
    private UmsMemberReceiveAddressMapper addressMapper;
    @Override
    public int add(UmsMemberReceiveAddress address) {
//        当这个地址是默认地址,设置其他地址为非默认地址
        log.info(address.toString());
        if (address.getIsDefault() == 1){
            UmsMemberReceiveAddressExample example = new UmsMemberReceiveAddressExample();
            example.createCriteria().andUserIdEqualTo(address.getUserId());
            UmsMemberReceiveAddress temp = new UmsMemberReceiveAddress();
            temp.setIsDefault(0);
            int result = addressMapper.updateByExampleSelective(temp, example);
        }

        return addressMapper.insert(address);
    }

    @Override
    public int delete(Long addressId) {
        UmsMemberReceiveAddressExample example = new UmsMemberReceiveAddressExample();
        example.createCriteria().andIdEqualTo(addressId);
        return addressMapper.deleteByExample(example);
    }

    @Override
    public int update (UmsMemberReceiveAddress address) {
        UmsMemberReceiveAddressExample example = new UmsMemberReceiveAddressExample();
        if (address.getIsDefault() == 1){

            example.createCriteria().andUserIdEqualTo(address.getUserId());
            UmsMemberReceiveAddress temp = new UmsMemberReceiveAddress();
            temp.setIsDefault(0);
            addressMapper.updateByExampleSelective(temp, example);

        }
        example = new UmsMemberReceiveAddressExample();
        example.createCriteria().andIdEqualTo(address.getId());

        return addressMapper.updateByExampleSelective(address,example);
    }

    @Override
    public List<UmsMemberReceiveAddress> list(Long userId) {
        UmsMemberReceiveAddressExample example = new UmsMemberReceiveAddressExample();
        example.createCriteria().andUserIdEqualTo(userId);
        return addressMapper.selectByExample(example);
    }

    @Override
    public UmsMemberReceiveAddress getItem(Long addressId) {
        UmsMemberReceiveAddressExample example = new UmsMemberReceiveAddressExample();
        example.createCriteria().andIdEqualTo(addressId);
        List<UmsMemberReceiveAddress> addressList = addressMapper.selectByExample(example);
        if(!CollectionUtils.isEmpty(addressList)){
            return addressList.get(0);
        }
        return null;
    }
}